﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DWEIP.Models.Sys;

namespace DWEIP.Web.Core
{
    public static class ExtendMvcHtml
    {

        //下拉
        public static MvcHtmlString SwitchDropdown(this HtmlHelper helper, string name, bool check, string ontext, string offtext)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\"><option {1} value=\"true\">{2}</option><option {3} value=\"false\">{4}</option></select>",
                name, check ? "selected=\"selected\"" : "", ontext == "" ? "启用" : ontext, !check ? "selected=\"selected\"" : "", offtext == "" ? "禁用" : offtext);

            return new MvcHtmlString(sb.ToString());

        }
        //暂时弃用有BUG
        public static MvcHtmlString SwitchButtonByEdit(this HtmlHelper helper, string name, bool check)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<input class=\"easyui-switchbutton\" style=\"width:43px;\" value=\"{0}\" ontext=\"\" id=\"{1}\" name=\"{2}\" offtext=\"\" {3}>", (!check ? "false" : "true"), name, name, (!check ? "" : "checked"));

            return new MvcHtmlString(sb.ToString());

        }
        //暂时弃用有BUG
        public static MvcHtmlString SwitchButtonByEdit(this HtmlHelper helper, string name, bool check, string ontext, string offtext, string width)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<input class=\"easyui-switchbutton\" value=\"{0}\"  id=\"{1}\" name=\"{2}\" {3} offtext=\"{4}\" ontext=\"{5}\"  style=\"width:{6}px;\">", (!check ? "false" : "true"), name, name, (!check ? "" : "checked"), ontext, offtext, width);

            return new MvcHtmlString(sb.ToString());

        }
        /// <summary>
        /// 权限按钮
        /// </summary>
        /// <param name="helper">htmlhelper</param>
        /// <param name="id">控件Id</param>
        /// <param name="icon">控件icon图标class</param>
        /// <param name="text">控件的名称</param>
        /// <param name="perm">权限列表</param>
        /// <param name="keycode">操作码</param>
        /// <param name="hr">分割线</param>
        /// <returns>html</returns>
        public static MvcHtmlString ToolButton(this HtmlHelper helper, string id, string icon, string text, ref List<permModel> perm, string keycode, bool hr)
        {
            if (perm == null)
            {
                string filePath = HttpContext.Current.Request.FilePath;
                perm = (List<permModel>)HttpContext.Current.Session[filePath];
            }
            if (perm != null && perm.Where(a => a.KeyCode == keycode).Count() > 0)
            {
                StringBuilder sb = new StringBuilder();
                //sb.AppendFormat("<a id=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\">", id);
                //sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" style=\"font-size:14px\">", icon);
                //sb.AppendFormat("</span><span style=\"font-size:12px\">{0}</span></span></a>", text);
                //if (hr)
                //{
                //    sb.Append("<div class=\"datagrid-btn-separator\"></div>");
                //}

                sb.AppendFormat("<a id=\"{0}\" class=\"btn btn-default\">", id);
                sb.AppendFormat("<span class=\"{0}\"></span>&nbsp;{1}</a>", icon, text);

                return new MvcHtmlString(sb.ToString());
            }
            else
            {
                return new MvcHtmlString("");
            }
        }

        public static MvcHtmlString ToolButtonModel(this HtmlHelper helper, string id, string icon, string text, ref List<permModel> perm, string keycode, bool hr)
        {
            StringBuilder sb = new StringBuilder();
            if (perm == null)
            {
                string filePath = HttpContext.Current.Request.FilePath;
                perm = (List<permModel>)HttpContext.Current.Session[filePath];
            }
            if (perm != null && perm.Where(a => a.KeyCode == keycode).Count() > 0)
            {
                sb.AppendFormat("<a id=\"{0}\" class=\"btn btn-default\">", id);
                sb.AppendFormat("<span class=\"{0}\"></span>&nbsp;{1}</a>", icon, text);
                return new MvcHtmlString(sb.ToString());
            }
            else
            {
                return new MvcHtmlString("");
            }
        }
        /// <summary>
        /// 行内权限按钮
        /// </summary>
        /// <param name="helper">htmlhelper</param>
        /// <param name="id">控件Id</param>
        /// <param name="icon">控件icon图标class</param>
        /// <param name="text">控件的名称</param>
        /// <param name="perm">权限列表</param>
        /// <param name="keycode">操作码</param>
        /// <param name="hr">分割线</param>
        /// <returns>html</returns>
        public static MvcHtmlString ColumnsToolButton(this HtmlHelper helper, string fun, string icon, string text, List<permModel> perm, string keycode, bool hr)
        {
            if (perm.Where(a => a.KeyCode == keycode).Count() > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("<a onclick=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\">", fun);
                sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" style=\"padding-left: 20px;\">", icon);
                sb.AppendFormat("{0}</span></span></a>", text);
                if (hr)
                {
                    sb.Append("<div class=\"datagrid-btn-separator\"></div>");
                }
                return new MvcHtmlString(sb.ToString());
            }
            else
            {
                return new MvcHtmlString("");
            }
        }

        /// <summary>
        /// 普通按钮
        /// </summary>
        /// <param name="helper">htmlhelper</param>
        /// <param name="id">控件Id</param>
        /// <param name="icon">控件icon图标class</param>
        /// <param name="text">控件的名称</param>
        /// <param name="hr">分割线</param>
        /// <returns>html</returns>
        public static MvcHtmlString ColumnsToolButton(this HtmlHelper helper, string fun, string icon, string text, bool hr)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<a onclick=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\">", fun);
            sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" style=\"padding-left: 20px;\">", icon);
            sb.AppendFormat("{0}</span></span></a>", text);
            if (hr)
            {
                sb.Append("<div class=\"datagrid-btn-separator\"></div>");
            }
            return new MvcHtmlString(sb.ToString());

        }
        /// <summary>
        /// 普通按钮
        /// </summary>
        /// <param name="helper">htmlhelper</param>
        /// <param name="id">控件Id</param>
        /// <param name="icon">控件icon图标class</param>
        /// <param name="text">控件的名称</param>
        /// <param name="hr">分割线</param>
        /// <returns>html</returns>
        public static MvcHtmlString ToolButton(this HtmlHelper helper, string id, string icon, string text, bool hr)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<a id=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\">", id);
            sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" style=\"font-size:14px\">", icon);
            sb.AppendFormat("</span><span style=\"font-size:12px\">{0}</span></span></a>", text);
            if (hr)
            {
                sb.Append("<div class=\"datagrid-btn-separator\"></div>");
            }
            return new MvcHtmlString(sb.ToString());

        }
    }
}